package com.code.controller;

import com.alibaba.fastjson.JSONObject;
import com.code.domain.Activity;
import com.code.domain.Dish;
import com.code.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ActivityController {
    @Autowired
    ActivityService activityService;
    @RequestMapping("activity/add")
    public Object addActivity(Double full,Double decrease){
        JSONObject jsonObject=new JSONObject();
        if (activityService.addActivity(full,decrease)>0){
            jsonObject.put("code" ,"添加成功");
        }else {
            jsonObject.put("code" ,"添加失败");
        }
        return jsonObject;
    }
    @RequestMapping("activity/delete")
    public Object deleteActivity(Integer activityId){
        JSONObject jsonObject=new JSONObject();
        if ( activityService.deleteActivity(activityId) >0 ){
            jsonObject.put("code" ,"删除成功");
        }else {
            jsonObject.put("code", "删除失败");
        }
        return jsonObject;
    }
    @RequestMapping("activity/update")
    public Object updateActivity(Integer activityId,Double full,Double decrease){
        JSONObject jsonObject=new JSONObject();
        Activity activity=activityService.search(activityId);
        if ( activity!=null ){
            Activity activity1=new Activity(activityId,full,decrease);
            activityService.updateActivity(activity1);
            jsonObject.put("code" ,"更新成功");
        }else {
            jsonObject.put("code", "更新失败");
        }
        return jsonObject;
    }
    @RequestMapping("activity/search")
    public Object searchActivity(Integer activityId){
        JSONObject jsonObject=new JSONObject();
        Activity activity=activityService.search(activityId);
        if ( activity!=null ){
            jsonObject.put("activity" ,activity);
        }else {
            jsonObject.put("activity", "无结果,请检查id");
        }
        return jsonObject;
    }
    @RequestMapping("activity/all")
    public Object allActivity (){
        JSONObject jsonObject=new JSONObject();
        List<Activity> list=activityService.searchAll();
        if (list.isEmpty()){
            jsonObject=null;
        }else {
            jsonObject.put("list",list);
        }
        return jsonObject;
    }
}
